///********************************************************************
///* Copyright © 2011 fishr (fishr.flash@gmail.com)  
///********************************************************************


package su.fishr.effects.prismavision.views 
{
	import flash.events.Event;
	import flash.events.EventDispatcher;
	import flash.events.IEventDispatcher;
	
   /**
	 *  Управляет группой объектов призма. В асинхрон-
	 *	ном режиме дифферинцированно распределяет запуск поворота призм.
	 *	Необходимость создания продиктована возможностью деления
	 *	изображения на несколько рядов.
	 * 
	 * @version                1.0
	 * @playerversion          Flash 9
	 * @langversion            3.0
	 * @author                 fishr
	 * @created                16.08.2011 17:45
	 * @since                  16.08.2011 17:45
	 */
	internal class RowsControll extends EventDispatcher
	{
	/**-------------------------------------------------------------------------------
	* 
	*	   						V A R I A B L E ' S 	
	* 
	* --------------------------------------------------------------------------------
	*/
	//{
	    static public const AUTHOR:String = "fishr (fishr.flash@gmail.com)";
		static public const ROW_STOP:String = "rowStop";
		private var _row:Array;
		private var _asyn:int;
		private var _countFrame:int;
		private var _vertical:Boolean;
		private var _fwdParams:Array;
		private var _backParams:Array;
	//}
	
	/**-------------------------------------------------------------------------------
	* 
	*	 						P R O P E R T I E S 	
	* 
	* --------------------------------------------------------------------------------
	*/
	//{
	
	//}
	/**-------------------------------------------------------------------------------
	* 
	*								P U B L I C 	
	* 
	* --------------------------------------------------------------------------------
	*/
	//{
		public function RowsControll(row:Array, target:IEventDispatcher = null)
		{
			super(target);
			_row = row;

		}
		
		public function rotationRow(asyn:int, vertical:Boolean, fwdParams:Array, backParams:Array = null):void
		{
			_asyn = asyn;
			_countFrame = 0;
			_vertical = vertical;
			_fwdParams = fwdParams;
			_backParams = backParams;
		
			if ( asyn ) 
			{
				_row[0].addEventListener( Event.ENTER_FRAME, enterFrameHandler );
				_row[0].addEventListener( Prisma.ROTATION_STOP, rotationStopHandler);
			}
			else
			{
				for ( var i:int = 0; i < _row.length; i++ )
				{
					if ( backParams && ((i / 2) is int))_row[i].rotationPrisma( backParams, vertical);
					else _row[i].rotationPrisma( fwdParams, vertical);
					
					_row[i].addEventListener( Prisma.ROTATION_STOP, rotationStopHandler);
				}
			}
			
		}
		
		
		
		
	//}
	
	/**-------------------------------------------------------------------------------
	* 
	*								P R I V A T E 	
	* 
	* --------------------------------------------------------------------------------
	*/	
	//{
		private function enterFrameHandler(e:Event):void 
		{

			var startElem:Number = _countFrame / _asyn;
			
			_countFrame++;
			
			if ( startElem !== int( startElem ) && _countFrame > 1) return;
			
			startElem = Math.floor( startElem );
			
			if ( startElem === _row.length - 1 ) 
			{
				_row[0].removeEventListener( Event.ENTER_FRAME, enterFrameHandler );
				_countFrame = 0;
				
			}
			

			if ( _backParams && ((startElem / 2) is int ))_row[ startElem ].rotationPrisma( _backParams, _vertical);
			else _row[ startElem ].rotationPrisma( _fwdParams, _vertical);

			_row[ startElem ].addEventListener( Prisma.ROTATION_STOP, rotationStopHandler);
			
			

			
		}
		
		private function rotationStopHandler(e:Event):void 
		{
			if ( e.target === _row[ _row.length - 1 ] ) dispatchEvent( new Event( ROW_STOP ));
			e.target.removeEventListener(Prisma.ROTATION_STOP, rotationStopHandler);
		}
	//}
		
		
	}

}